package org.etom.impl.ui.actions;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Shell;
import org.etom.impl.ui.Activator;
import org.etom.impl.ui.common.IconConstants;
import org.etom.impl.ui.common.ResourcesTreeViewer;
import org.etom.impl.ui.views.ViewsMessages;
import org.etom.impl.ui.wizards.NewAccountWizard;

/**
 * An action class which is responsible for the creation
 * of the new connection.
 */
public class NewAccountAction extends Action {
  
  private Shell shell;
  private ResourcesTreeViewer resourcesTreeViewer;

  /**
   * Constructs a new account action.
   * 
   * @param shell - the shell will be used in the wizards
   * @param provider - the resource provider
   */
  public NewAccountAction(Shell shell, ResourcesTreeViewer resourcesTreeViewer) {
    this.shell = shell;
    this.resourcesTreeViewer = resourcesTreeViewer;
    init();
  }
  
  /**
   * @see org.eclipse.jface.action.Action#run()
   */
  @Override
  public void run() {
    NewAccountWizard wizard = new NewAccountWizard(null);
    WizardDialog wizardDialog = new WizardDialog(shell, wizard);
    wizardDialog.setBlockOnOpen(true);
    if (wizardDialog.open() == SWT.CANCEL) return;
    resourcesTreeViewer.addMoodleConnection(wizard.getMoodleConnection());
  }

  /*
   * The method initializes the action. 
   */
  private void init() {
    setText(ViewsMessages.Resources_Context_Menu_New);
    setImageDescriptor(ImageDescriptor.createFromFile(Activator.class,
            IconConstants.MOODLE_CONNECTION_ICON_PATH));
  }
  
}
